﻿using System;
using System.Web.Security;
using System.Web.UI;
using Be;
using WebGui.BllLessonService;


namespace WebGui.Admin {

    public partial class Edit : Page {

        /// <summary>
        /// The LessonServiceClient
        /// </summary>
        private LessonServiceClient _lesson;

        /// <summary>
        /// The event
        /// </summary>
        private BeLesson _event;

        /// <summary>
        /// Loads the page
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event arguments</param>
        protected void Page_Load(object sender, EventArgs e) {
            try {
                _lesson = (LessonServiceClient) Session["Lesson"];
                if(!IsPostBack) {
                    int id = Convert.ToInt32(Request.QueryString["id"]);
                    loadEvent(id);
                    Session["userid"] = _event.UserId;
                    TxtStart.Text = _event.StartTime.ToString();
                    TxtEnd.Text = _event.EndTime.ToString();
                    TxtComment.Text = _event.Comments;
                    DdlType.SelectedValue = _event.TypeId.ToString();
                    TxtComment.Focus();
                }
            } catch(Exception ex) {
                Response.Write("<script type='text/javascript'>alert('" + ex.Message + "'); </script>");
            }
        }

        /// <summary>
        /// Load a lesson
        /// </summary>
        /// <param name="id">The if of the lesson</param>
        private void loadEvent(int id) {
            try {
                _event = _lesson.GetLesson(id);
            } catch(Exception ex) { Response.Write("<script type='text/javascript'>alert('" + ex.Message + "'); </script>"); }
        }

        /// <summary>
        /// The cancel button click
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event arguments</param>
        protected void BtnCancel_Click(object sender, EventArgs e) {
            Modal.Close(this);
        }

        /// <summary>
        /// The ok button click
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event arguments</param>
        protected void BtnOk_Click(object sender, EventArgs e) {
            try {
                int id = Convert.ToInt32(Request.QueryString["id"]);
                DateTime start = Convert.ToDateTime(TxtStart.Text);
                DateTime end = Convert.ToDateTime(TxtEnd.Text);
                int Type = Convert.ToInt32(DdlType.SelectedValue);
                string comment = TxtComment.Text;
                BeLesson editlesson = new BeLesson(id, start, end, Membership.GetUser().ProviderUserKey.ToString(), comment, Type);
                _lesson.EditLesson(editlesson);
                Modal.Close(this, "OK");
            } catch(Exception ex) { Response.Write("<script type='text/javascript'>alert('" + ex.Message + "'); </script>"); }
        }
    }
}